(Timisoara, pregatire, feb. 1996)
   Dandu-se o secventa x_1, x_2, x_3,...,x_n de numere reale sa se gaseasca
o subsecventa de elemente consecutive x_i, x_i+1, ...,x_k astfel incat suma
acestor elemente este maximala in raport cu toate subsecventele de elemente
consecutive (x_i este x indice i).
   Datele  de intrare  sunt date in fisierul int.txt astfel: o linie
contine numarul de elemente (n) iar urmatoarea linie elementele.
   Rezultatelor vor fi obtinute in fisierul out.txt pe cate o linie.
Exemplu
Fisierul int.txt
8
2 -3 1.5 -1 3 -2 -3 3

Fisierul out.txt
Suma=3.5 Secventa: 1.5; -1; 3
-----------------------------------------------------------------------------
(Cu conditia suplimentara ca cel un numar este pozitiv, s-a dat la Chisinau,
etapa finala 1993)

================================================
Solutia 1 (Angel Proorocu - Ploiesti):
program SecvantaMaxima;

   uses Crt;

   var a,b:array[1..5000]of real;
       n:integer;
       f:text;
       numei,numeo:string;

procedure ReadData;
   var i:integer;
   begin
     clrscr;
     writeln('Care este numele fisierului de intrare ?');
     readln(numei);
     writeln('Care exte numele fisierului de iesire ?');
     readln(numeo);
     assign(f,numei);
     reset(f);
     readln(f,n);
     for i:=1 to n do read(f,a[i]);
     close(f);
   end;

procedure Rezolva;
   var i,j:integer;
       suma:real;
   begin
    for i:=n downto 1 do
     begin
      b[i]:=a[i];
      suma:=a[i];
      for j:=i+1 to n do
       begin
         if b[i]<a[j]+suma then b[i]:=a[j]+suma;
         suma:=suma+a[j];
       end;
     end;
    j:=1;
    suma:=b[1];
    for i:=2 to n do if b[i]>suma then begin j:=i; suma:=b[i]; end;
    assign(f,numeo);
    rewrite(f);
    writeln(f,'Suma maxima: ',suma:2:2);
    writeln(f,'Secventa:');
    i:=j;
    suma:=0;
    while suma<>b[j] do
     begin
      write(f,a[i]:2:2,' ');
      suma:=suma+a[i];
      i:=i+1;
     end;
    close(f);
    writeln('Apasa o tasta..');
    readkey;
  end;

begin
  ReadData;
  Rezolva;
end.
----------------------------------
Solutia 2 (Bogdan Batog)
type
    vect        =array[0..10000] of real;
var
   fil          :text;
   A,S          :^vect;
   M            :array[0..10000] of integer;
   n,i,j,f      :integer;
   smax         :real;

begin
     assign(fil,'int.txt');
     reset(fil);
     readln(fil,n);
     M[0]:=1;
     S^[0]:=0;
     for i:=1 to n do
     begin
          read(fil,A^[i]);
          S^[i]:=S^[i-1]+A^[i];
          M[i]:=M[i-1];
          if S^[M[i]]>S^[i] then M[i]:=i;
     end;
     close(fil);
     M[1]:=0;
     smax:=-maxlongint;
     for f:=1 to n do
     if S^[f]-S^[M[f]]>smax then
     begin
          smax:=S^[f]-S^[M[f]];
          i:=M[f]+1;
          j:=f;
     end;
     writeln('suma:',smax:4:4);
     writeln('indexi: i1=',i,'  i2=',j);
end.
---------------------
Solutia 3 (Bogdan Batog)
{ Se genereaza un vector s,s[i] fiind suma primelor i nr. din vector.
Suma secventei de la i la j va fi s(j)-s(i-1) }
uses crt;
type
    vector=array[0..10000] of real;
var
   a,s:^vector;
   i,j,k,n,i1,i2,j1:integer;
   max:real;
   st:string;
   f:text;

begin
     clrscr;
     write('Fisier intrare:');
     readln(st);
     assign(f,st);
     reset(f);
     readln(f,n);
     new(a);new(s);
     s^[0]:=0;
     for i:=1 to n do
         begin
              read(f,a^[i]);
              s^[i]:=s^[i-1]+a^[i];
         end;
     close(f);
     {--}
     max:=-maxlongint;
     for i:=1 to n do
         for j:=i to n do
             if s^[j]-s^[i-1]>max
                then
                    begin
                         max:=s^[j]-s^[i-1];
                         i1:=i;
                         j1:=j;
                    end;
     writeln('Suma:',max:6:2);
     write('Secventa:');
     for i:=i1 to j1 do
         write(a^[i]:6:2,' ');
     writeln;
     readkey;
end.
---------------------------
Solutia 4 (Adrian carcu)
var fis:text;
    x,s:array[0..100] of real;
    sum,max:real;
    i,j,ii,jj,n:integer;

function format(n:real):string;
var s:string;
begin
   str(n:0:20,s);
   while s[length(s)]='0' do dec(s[0]);
   if s[length(s)]='.' then dec(s[0]);
   format:=s;
end;

begin
   assign(fis,'secv_max.dat'); reset(fis);
   readln(fis,n);
   for i:=1 to n do read(fis,x[i]);
   close(fis);
   sum:=0; s[0]:=0;
   for i:=1 to n do begin
      sum:=sum+x[i];
      s[i]:=sum;
      end;
   max:=0;
   for i:=0 to n-1 do
      for j:=i+1 to n do
         if s[j]-s[i]>max then begin max:=s[j]-s[i];  ii:=i; jj:=j; end;
   write('Suma:',format(max),' Secventa:');
   for i:=ii+1 to jj do write(format(x[i]),' ');
   writeln;
end.
--------------------------
Solutia 5 (Tudor Leu)
{
 Fara conditia suplimentara
}
var ni,no:string;
    a:array[1..1000] of real;
    n,i,j,k,l:word;
    max,s:real;
begin
     write('Intrare : ');readln(ni);
     write('Iesire  : ');readln(no);
     assign(input,ni);reset(input);
     readln(n);
     for i:=1 to n do read(a[i]);
     max:=-1;k:=0;l:=0;s:=0;j:=1;
     for i:=1 to n do begin
        s:=s+a[i];
        if s<0 then begin s:=0;j:=i+1 end
               else if s>max then begin max:=s;k:=j;l:=i-j end;
     end;
     assign(output,no);rewrite(output);
     if max>0 then begin
                     write('Suma=',max:10:2,'. Secventa : ');
                     for i:=k to k+l do write(a[i]:8:2);
                   end
              else begin
                     max:=-1000000;
                     for i:=1 to n do if a[i]>max then max:=a[i];
                     write('Suma= ',max:10:2,'. Secventa : ',max:8:2);
                   end;
end.
----------------------------
Solutia 6 (Virgil Serbanuta)
uses crt;
var sc,sm,n,max:real;
    ic,im,pc,sfm,pm,nrel:longint;

begin
clrscr;
sm:=0;
max:=-1.7e38;
sc:=0;
ic:=0;
pm:=0;
assign(input,'Secv_max.in');
reset(input);
readln(nrel);
for pc:=1 to nrel do
     begin
     read(n);
     if sc=0 then
        if n>0 then
           begin
           sc:=n;
           ic:=pc;
           end else
        else begin
        sc:=sc+n;
        if sc<0 then sc:=0;
        end;
     if max<n then begin
        max:=n;
        pm:=pc;
        end;
     if sm<sc then
        begin
        sm:=sc;
        im:=ic;
        sfm:=pc;
        end;
     end;
if sm=0 then
   begin
   writeln('Suma: ',max,' Secventa : ',max);
   halt;
   end;
close(input);
assign(input,'Secv_max.in');
reset(input);
readln(nrel);
write('Suma: ',sm,' Secventa: ');
for pc:=1 to im do read(n);
write(n,' ');
for pc:=im+1 to sfm do
    begin
    read(n);
    write(n,' ');
    end;
end.
---------------------------
